package search.users;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONException;
import org.json.JSONObject;

import server.Initialize;
import server.db.StmtHelper;
import server.json.FunctionalJSON;

import ajaxmethods.AjaxMethod;

public class AgentProfile implements AjaxMethod {

	@Override
	public void perform(HttpServletRequest request, HttpServletResponse response) {
		response.setContentType("text/json");
		
		JSONObject agent = getAgent(request.getParameter("id"));
		StringBuffer sb = new StringBuffer();
		try {
			sb.append("<div id=\"photo-block\"><img class=\"agentSidePhoto\" src=\""+Initialize.appURL()+"/"+agent.get("image_url")+"\" " +
			"class=\"photograybox\"><div id=\"contactinfo\">" +
			"<div>"+agent.get("fullname")+"</div><p class=\"translate-static\">" +
			"<a class=\"email-agent-link\" href=\"mailto:"+agent.get("emailaddress")+"?Subject=Hi%20I%20Found%20Your%20Email%20Through%20Listings%20Connection\">Email Me</a></p>" +
			"<p class=\"translate-static\">" +
			"<a class=\"email-agent-link\" href=\"#!/profile/"+agent.get("contextpath")+"\" rel=\"/profile/"+agent.get("contextpath")+"\" >See My Listings</a>" +
			"</p><div class=\"agentlink-separator\"></div><p>" +
			"<strong class=\"translate-static title\">Direct:&nbsp;</strong>"+
			agent.get("directline")+"</p><p><strong class=\"translate-static title\">Office:&nbsp;</strong>"+
			agent.get("officephone")+"</p><p><strong class=\"translate-static title\">Mobile:&nbsp;</strong>"+
			agent.get("mobile")+"</p><p><strong class=\"translate-static title\">Fax:&nbsp;</strong>" +
			agent.get("fax")+"</p><div class=\"agentlink-separator\"></div>" +
			"<p class=\"title\"><b>"+agent.get("officename")+"</b></p>" +
			"<p>"+agent.get("officeaddress")+"</p>" +
			"<div style=\"border-bottom:1px solid ##9DA5A4;margin: 5px 0 10px 0;\"></div></div>");
			new JSONObject().put("htmlSelector", "#agentWorkspace #noscrollsidepanel").put("htmlValue", sb.toString())
				.write(response.getWriter());
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	

	public static JSONObject getAgent(String idagents) {
		try {
			return FunctionalJSON.mapToJSONObject.f(StmtHelper.db("select image_url,fullname,contextpath,agentid,directline,officephone,mobile,fax,officename,officeaddress,emailaddress from agents where idagents=").addVariable(idagents)
				.collectIntoMaps.unsafeGet());
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

}
